<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
	/**
     * Initialize autoloader
     *
     * @return void
     */
    protected function _initAutoload()
    {
        $autoloader = Zend_Loader_Autoloader::getInstance();
        $autoloader->registerNamespace('Itc_')
                   ->suppressNotFoundWarnings(true);

        $defaultAutoloader = new Zend_Application_Module_Autoloader(array(
            'namespace' => 'Itc_',
            'basePath'  => APPLICATION_PATH
        ));

        // need this to extend all application controllers from abstract
        require_once APPLICATION_PATH . '/controllers/AbstractController.php';

        $autoloader->setAutoloaders(array(
            'default' => $defaultAutoloader,
        ));

        return $autoloader;
    }

    /**
     * Initialize config
     *
     * @return void
     */
    protected function _initConfig()
    {
        $conf = Itc_Config_Ini::get(APPLICATION_PATH . '/configs/application.ini', APPLICATION_ENV, array(
            'allowModifications' => true
        ));
        Zend_Registry::set('Config', $conf);
    }
    
    /**
     * Init plugins (Auth, Acl)
     */
    protected function _initPlugins()
    {
        $acl = new Itc_Acl($this->getOptions());
        Zend_Registry::set('Acl', $acl);

        $front = Zend_Controller_Front::getInstance();
        $front->registerPlugin(new Itc_Plugin_Auth($acl));
    }
    
    /**
     * Init route data
     */
    protected function _initRouter()
    {
        Zend_Controller_Front::getInstance()->setDefaultModule('default');
    }
    
    /**
     * Initialize Dwoo templates engine
     *
     * @return void
     */
    protected function _initDwoo()
    {
        $this->bootstrap('view');

        $config = $this->getOptions();

        $view = new Dwoo_Adapters_ZendFramework_View(array(
            'engine' => array(
                'compileDir' => $config['resources']['view']['compile_dir']
            )
        ));

        $view->addScriptPath(array (
                APPLICATION_PATH . '/views/scripts'
             ))
             ->addHelperPath('views/helpers', 'Itc_View_Helper_');

        // Helpers
        $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer($view);
        $viewRenderer->setViewSuffix('tpl');

        Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
    }
    
    /**
     * Init Resources, layout
     */
    protected function _initResources()
    {
        $this->bootstrap('layout');
        $layout = $this->getResource('layout');
        $layout->setLayout('main');
    }

    /**
     * Init head part (Js, Css, meta, etc)
     */
    protected function _initHead()
    {
        $styles = $this->getOption('stylesheet');
        $jss    = $this->getOption('js');
        $title  = $this->getOption('app');
        $meta   = $this->getOption('meta');

        $view   = $this->getResource('view');
        $view->addHelperPath('Itc/View/Helper/', 'Itc_View_Helper_');

        $view->getHelper('doctype')->setDoctype($this->getOption('doctype'));

        // Add tags <META>
        $metaObj = $view->getHelper('HeadMeta');
        foreach ($meta as $method => $array) {
            $method = 'append' . $method;
            foreach ($array as $type => $value) {
                $metaObj->$method($type, $value);
            }
        }

        // Add tag <TITLE>
        $view->getHelper('HeadTitle')->headTitle(
            $title['title'],
            Zend_View_Helper_Placeholder_Container_Abstract::SET
        );

        // Add tag <LINK>
        $cssHelper = $view->getHelper('HeadLink');
        foreach ($styles as $css) {
            $cssHelper->appendStylesheet($css);
        }

        // Add tag <SCRIPT>
        $jsHelper = $view->getHelper('HeadScriptProxy');
        foreach ($jss as $js) {
            $jsHelper->appendFile($js);
        }
    }

    /**
     * Initialize Cache
     */
    protected function _initCacheManager()
    {
        $this->bootstrap('cache');
        $cache = $this->getResource('cache');
    }

}

